//
//  JDKJAlertView.m
//  WNZSSQ
//
//  Created by JDKJ on 2018/12/6.
//  Copyright © 2018年 JDKJ. All rights reserved.
//

#import "JDKJAlertView.h"

@implementation JDKJAlertView

- (instancetype)initWithFrame:(CGRect)frame
{
    CGRect initialFrame;
    
    if (CGRectIsEmpty(frame)) {
        
        initialFrame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
        
    } else {
        
        initialFrame = frame;
    }
    
    self = [super initWithFrame:initialFrame];
    
    if (self) {
        
        
        UIView *alphaView = [[UIView alloc] init];
        alphaView.backgroundColor = [CSBlackColor colorWithAlphaComponent:0.7];
        [self addSubview:alphaView];
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(hiddenWithAnimation)];
        [alphaView addGestureRecognizer:tap];
        self.alphaView = alphaView;
        
        UIView *whiteView = [[UIView alloc] init];
        whiteView.layer.cornerRadius = 10;
        whiteView.backgroundColor = [CSWhiteColor colorWithAlphaComponent:1];
        [self addSubview:whiteView];
        self.whiteView = whiteView;
        
        
        
        UILabel *titleLabel = [[UILabel alloc] init];
        titleLabel.text = @"确定删除这本书吗？";
        titleLabel.font = MidFont;
        titleLabel.textColor = CSBlackColor1;
        [whiteView addSubview:titleLabel];
        self.titleLable = titleLabel;
        
        /**
         左按钮
         */
        UIButton *leftBtn = [[UIButton alloc] init];
        leftBtn.backgroundColor = CSWhiteColor;
        [leftBtn addTarget:self action:@selector(leftBtnClick)];
        [leftBtn setTitle:@"删除"];
        leftBtn.titleLabel.font = kFont15;
        [leftBtn setTitleColor:CSColorFromRGB(0x6B6763)];
        leftBtn.layer.borderWidth = 1;
        leftBtn.layer.borderColor = LineBackGroundColor.CGColor;
        leftBtn.layer.cornerRadius = 5;
        leftBtn.clipsToBounds = YES;
        [whiteView addSubview:leftBtn];
        self.leftBtn = leftBtn;
        
        /**
         右按钮
         */
        UIButton *rightBtn = [[UIButton alloc] init];
        rightBtn.backgroundColor = CSWhiteColor;
        [rightBtn addTarget:self action:@selector(rightBtnClick)];
        rightBtn.titleLabel.font = kFont15;
        [rightBtn setTitle:@"取消"];
        [rightBtn setTitleColor:CSWhiteColor];
        rightBtn.layer.cornerRadius = 5;
        rightBtn.clipsToBounds = YES;
        [whiteView addSubview:rightBtn];
        self.rightBtn = rightBtn;
        
        /**
         退出
         */
//        UIButton *closeBtn = [[UIButton alloc] init];
//        closeBtn.backgroundColor = CSWhiteColor;
//        [closeBtn addTarget:self action:@selector(hiddenWithAnimation)];
//        [closeBtn setImage:@"pop_icon_close"];
//        closeBtn.contentEdgeInsets = UIEdgeInsetsMake(5, 5, 5, 5);
//        [whiteView addSubview:closeBtn];
//        self.popBtn = closeBtn;
        
        
        
        [self setConstraints];
        
    }
    
    return self;
}

-(void)setConstraints
{
    
    
    
    /**
     背景色
     */
    [self.alphaView mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        make.edges.mas_equalTo(self);
    }];
    
    /**
     内容view
     */
    [self.whiteView mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        make.center.mas_equalTo(self);
        make.right.mas_equalTo(self).offset(-45);
        make.left.mas_equalTo(self).offset(45);
    }];
    
    
    /**
     标题
     */
    
    [self.titleLable mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.whiteView).offset(24);
        make.centerX.mas_equalTo(self.whiteView);
        make.left.mas_greaterThanOrEqualTo(self.whiteView).offset(10);
        make.right.mas_lessThanOrEqualTo(self.whiteView).offset(-10);
    }];
    
    [self.leftBtn mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.titleLable.mas_bottom).offset(30);
        make.bottom.mas_equalTo(self.whiteView).offset(-25);
        make.right.mas_equalTo(self.whiteView.mas_centerX).offset(-20);
        make.width.mas_equalTo(80);
        make.height.mas_equalTo(40);
    }];
    
    [self.rightBtn mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(self.leftBtn);
        make.left.mas_equalTo(self.whiteView.mas_centerX).offset(20);
        make.width.mas_equalTo(80);
        make.height.mas_equalTo(40);
    }];
    
    
  
    /**
     退出
     */
//    [self.popBtn mas_remakeConstraints:^(MASConstraintMaker *make) {
//        make.top.mas_equalTo(self.whiteView);
//        make.right.mas_equalTo(self.whiteView);
//        make.width.height.mas_equalTo(30);
//    }];
    
    
    
    
    
}


-(void)leftBtnClick
{
    if (self.leftClick) {
        self.leftClick();
    }
    
    [self hiddenWithAnimation];
}

-(void)rightBtnClick
{
    if (self.rightClick) {
        self.rightClick();
    }
    
    [self hiddenWithAnimation];
}

- (void)showWithAnimation {
    
    
    
    [self addViews];
    
    CGRect frame = self.whiteView.frame;
    self.alphaView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.0];
    self.whiteView.frame = CGRectMake(0, SCREEN_HEIGHT, SCREEN_WIDTH, SCREEN_HEIGHT);
    
    [UIView animateWithDuration:0.3 animations:^{
        self.alphaView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.4];
        self.whiteView.y = frame.origin.y;
    }];
    
}


- (void)hiddenWithAnimation {
    CGFloat height = self.whiteView.frame.size.height;
    [UIView animateWithDuration:0.25 animations:^{
        self.whiteView.center = CGPointMake(SCREEN_WIDTH / 2, SCREEN_HEIGHT + height / 2);
        self.alphaView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.0];
    } completion:^(BOOL finished) {
        [self hiddenViews];
    }];
}

- (void)addViews {
    
    UIWindow *window = [UIApplication sharedApplication].keyWindow;
    
    [window addSubview:self];
}

- (void)hiddenViews {
    
    
    [self removeFromSuperview];
}


-(void)drawRect:(CGRect)rect
{
    CAGradientLayer *gradientLayer = [CAGradientLayer layer];
    gradientLayer.colors = @[
                             (id)CSColorFromRGB(0xF52C50).CGColor,(id)CSColorFromRGB(0xF95C48).CGColor
                             ,
                             ];
    gradientLayer.startPoint = CGPointMake(0, 0);
    gradientLayer.endPoint = CGPointMake(1.0, 0);
    gradientLayer.frame = self.rightBtn.bounds;
    [self.rightBtn.layer insertSublayer:gradientLayer atIndex:0];
}

@end
